﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Commons.DataFilters;
using SD.Commons;

namespace SD.Web.Views.Department
{
    /// <summary>
    /// The class which contains ListDepartment's business logic
    /// </summary>
    public partial class ListDepartment : System.Web.UI.Page
    {
        /// <summary>
        /// The method which executes needed on page loaded
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            var filter = new DepartmentFilter();
            ViewState["filter"] = filter;
            ViewState["sortingButton"] = LinkButtonAll.UniqueID;
        }

        /// <summary>
        /// The method which is invoked when data queried
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void ObjectDataSourceDepartmentList_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// The method which is invoked when user filter data
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            if (filter != null)
            {
                var beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);
                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.UniqueID;
                }
                GridViewDepartmentList.PageIndex = 0;
                GridViewDepartmentList.DataBind();
            }
        }

        /// <summary>
        /// The method which is invoked when checkbox in-active cliked
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            if (filter != null)
            {
                filter.IncludeInactive = CheckBoxInactive.Checked;
            }
            this.GridViewDepartmentList.DataBind();
        }

        /// <summary>
        /// The method which is invoked when "create" button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Views/Department/DepartmentDetail.aspx");
        }


    }
}